package com.Maykk.Demo09;

import java.io.*;
import java.net.Socket;

/**
 * @ClassName: ClientUpload
 * @Description: TODO 读取文件发送到 TCP
 * @Author: MaykkStar@163.com
 */
public class ClientUpload {
    static final int PORT = 9527;
    static final String FILENAME = "TCP_IO.txt";
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", PORT);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        File file = new File(FILENAME);

        BufferedReader fileReader = new BufferedReader(new FileReader(file));
        // 发送一个文件名过去
        bw.write(file.getName());
        bw.newLine();
        bw.flush();

        // 发送文件内容
        String line;
        while ((line = fileReader.readLine()) != null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        } // 在当前这个循环中，不可能把 null 写到服务器

        // 解决不会接收反馈的问题
        socket.shutdownOutput();

        // 接收服务器反馈
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println("接收到服务器反馈 > " + br.readLine());

        br.close();
        socket.close();
    }
}
